package movies.repositoryHibernate;

import java.util.List;
import movies.domain.Article;
import movies.repository.IArticleRepository;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hanna_Aliakseichykav
 */
public class ArticleRepository extends HibernateDaoSupport
        implements IArticleRepository{

    @Override
    public Article getArticleById(int id) {
        List < Article > articles = getHibernateTemplate().
             findByNamedQuery("GetArticleById", id);

        if (articles.size() == 1) {
            return articles.get(0);
        } else {
            return null;
        }
    }

    @Override
    public Article saveArticle(Article article) {
        Integer id = (Integer)getHibernateTemplate().save(article);
         article.setId(id);
         return article;
    }

    @Override
    public void deleteArticle(Article article) {
        getHibernateTemplate().delete(article);
    }

    @Override
    public void updateArticle(Article article) {
        getHibernateTemplate().update(article);
    }

    @Override
    public List<Article> getArticlesByArticleCategoryId(int articleCategoryId) {
       List < Article > articles = getHibernateTemplate().
             findByNamedQuery("GetArticlesByArticleCategoryId",
             articleCategoryId);

        if (articles.size() > 0) {
            return articles;
        } else {
            return null;
        }
    }

    @Override
    public List<Article> getArticlesByCategoryAndUserId
            (final int categoryId, final int userId) {
        List < Article > articles = getHibernateTemplate().
             findByNamedQuery("GetArticlesByCategoryAndUserId",
             new Integer[]{categoryId, userId});

        if (articles.size() > 0) {
            return articles;
        } else {
            return null;
        }
    }

    @Override
    public List<Article> getArticlesByNameEvent(String nameEvent) {
        List < Article > articles = getHibernateTemplate().
             findByNamedQuery("GetArticlesByNameEvent",
             nameEvent);

        if (articles.size() > 0) {
            return articles;
        } else {
            return null;
        }
    }

    @Override
    public List<Article> getArticlesByEventCategoryId(int eventCategoryId) {
        List < Article > articles = getHibernateTemplate().
             findByNamedQuery("GetArticlesByEventCategoryId",
             eventCategoryId);

        if (articles.size() > 0) {
            return articles;
        } else {
            return null;
        }
    }

    @Override
    public List<Article> getArticlesByCountryEventId(int countryEventId) {
        List < Article > articles = getHibernateTemplate().
             findByNamedQuery("GetArticlesByCountryEventId",
             countryEventId);

        if (articles.size() > 0) {
            return articles;
        } else {
            return null;
        }
    }

}
